/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.jrwz.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.model.L1World;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.serverpackets.S_SendLocation;
import l1j.jrwz.server.serverpackets.S_ServerMessage;

/**
 * Class <code>C_SendLocation</code> 请求 传送坐标位置&视窗失焦.
 * 
 * @author jrwz
 * @version 2012-12-7下午6:30:15
 * @see l1j.jrwz.server.clientpackets
 * @since JDK1.7
 */
public class C_SendLocation extends ClientBasePacket {
    private static final String C_SEND_LOCATION = "[C] C_SendLocation";
    private static Logger _log = Logger.getLogger(C_SendLocation.class
            .getName());
    private static final int SEND_LOCATION = 11;
    private static final int CLIENT_STATUS = 13;

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);
            int type = readC();
            if (type == CLIENT_STATUS) { // 客户端在主动,非主动转换时(窗口化鼠标点击游戏窗口内外、可以判断游戏在不在当前)
                /*
                 * 视窗内:0d 01 xx、视窗外:0d 00 xx // xx 就是值
                 */
                return;
            }

            if (type == SEND_LOCATION) {
                String targetName = readS();
                if (targetName.isEmpty()) {
                    return;
                }

                L1PcInstance target = L1World.getInstance().getPlayer(
                        targetName);
                if (target != null) {
                    L1PcInstance pc = client.getActiveChar();
                    if (pc == null) {
                        return;
                    }
                    String sender = pc.getName();
                    int mapId = readH();
                    int x = readH();
                    int y = readH();
                    int msgId = readC();
                    target.sendPackets(new S_SendLocation(sender, mapId, x, y,
                            msgId));
                    pc.sendPackets(new S_ServerMessage(1783, targetName)); // 完成传达给用户%0
                } else {
                    L1PcInstance pc = client.getActiveChar();
                    if (pc == null) {
                        return;
                    }
                    pc.sendPackets(new S_ServerMessage(1782)); // 无法找到相关角色。
                }
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_SEND_LOCATION;
    }

}
